package com.shizhanzhe.gxb.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.WXLoginActivity;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.home.adapter.RecommendAdapter;
import com.shizhanzhe.gxb.home.bean.ListBean;
import com.shizhanzhe.gxb.home.bean.Recommend;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * @author YJ
 * @date 2019/1/17
 */
public class RecommendActivity extends SwipeBackActivity {

    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.no_data)
    ImageView noData;
    private int type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarView(R.id.bar_0_view)
                .init();
        type = getIntent().getIntExtra("type", 0);
        switch (type) {
            case 0:
                topTitle.setText("1~9.9包邮");
                break;
            case 1:
                topTitle.setText("今日爆款");
                break;
            case 2:
                topTitle.setText("品牌清仓");
                break;
            default:
                break;
        }
        recommend();
    }

    @OnClick(R.id.backarea)
    public void onViewClicked() {
        finish();
    }

    private void recommend() {
        OkHttpDownloadJsonUtil.downloadJson(RecommendActivity.this, Path.recommend(type), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        final Recommend recommend = new Gson().fromJson(json, Recommend.class);
                        Log.d("____",recommend.getData().getGoods_basic_detail_response().getList().size()+"");
                        if (recommend.getData().getGoods_basic_detail_response().getList().size() > 0) {
                            final List<ListBean> beanList = new ArrayList<>();
                            for (ListBean bean : recommend.getData().getGoods_basic_detail_response().getList()){
                                double qhprice=((double) bean.getMin_group_price()-(double)bean.getCoupon_discount())/100;
                                if (qhprice<=10 && bean.getCoupon_discount()/100 != 0){
                                    beanList.add(bean);
                                }
                            }
                            RecommendAdapter recommendAdapter = new RecommendAdapter(beanList, RecommendActivity.this);
                            listview.setAdapter(recommendAdapter);
                            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    if (UserBean.uid != 0) {
                                        startActivity(new Intent(RecommendActivity.this,PDDDetailActivity.class).putExtra("goods_id",beanList.get(i).getGoods_id()));
                                    } else {
                                        startActivity(new Intent(RecommendActivity.this,WXLoginActivity.class));
                                    }
                                }
                            });
                        } else {
                            noData.setVisibility(View.VISIBLE);
                        }
                    } catch (Exception e){
                        Log.d("___________",e.toString());
                        Toast.makeText(RecommendActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(RecommendActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}